// 查余额的控制器
angular.module('accountbalance.controller', ['accountbalance.service'])
  .controller("AccountbalanceCtrl", function ($scope,$rootScope,$http,$filter) {
        //数据接口
        //页面加载，获取授权账户余额
        var CCSessionId =localStorage.getItem("CCSessionId");
        var url =$rootScope.myglobal.serverurl+'/jsye/jsye.json?CCSessionId='+CCSessionId;
        console.log(url);
        $http.get(url)
            .success(function(data){
                $scope.items = data;
                //获取账户id
                var acctIdsTemp = "";
                angular.forEach(data, function (item) {
                    var str = item[0]+",0";
                    acctIdsTemp += "acctIdStr="+str+"#"
                });
                //var acctIdStr = document.getElementsByClassName("acctIds");
                //var acctIdsTemp="";
                //for(var i=0;i< acctIdStr.length;i++){
                //    var str  =acctIdStr[i].innerText+",0"
                //    acctIdsTemp += "acctIdStr="+str+"#"
                //}
                //拼接参数（刷新参数acctIdsTemp）
                acctIdsTemp = acctIdsTemp.substring(0,acctIdsTemp.length-1);
                $scope.acctIdsTemp = acctIdsTemp;
                console.log($scope.acctIdsTemp);

            }).error(function(data){
                console.log(data);
            });

        //实现账户记录的单击
        var dataobj = {};
        $scope.Accountrecord = function (e) {
            console.log(e);
                //$scope.sw =!$scope.sw;
            console.log(0000);
            angular.forEach( $scope.items, function (item,index) {
                if(e == index){
                    console.log(item[0]);
                    var show = document.getElementById(index);
                    if(show.style.display=="none"){
                        show.style.display="block"
                    }else{
                        show.style.display="none"
                    }
                    console.log(item[0]);
                    var url =$rootScope.myglobal.serverurl+'/account/account.json?CCSessionId='+CCSessionId+"&acctId="+item[0]+"&systemFlag=2"
                    $http.get(url)
                        .success(function(data){
                            $scope.value =data;
                            console.log(data);
                            dataobj[e]=data;
                            $scope.data=dataobj;
                            //$scope.click =data;
                            //先判断error是否为null或空，若是则表示操作成功取整个对象作为范围结果，若否则终止当前操作。
                            if(data==null||data==" "){
                                return false
                            }
                            angular.forEach(data,function (item,index) {
                                //先判断是否为null或空，是则取0.00;否则采用千分位计数法显示金额。
                                if(item.contributionAmount ==null||item.contributionAmount ==""){
                                    item.contributionAmount ="0.00"
                                }
                                //先判断是否为null或空，是则取0.00;否则采用千分位计数法显示金额。
                                if(item.managedAmount ==null||item.managedAmount ==""){
                                    item.managedAmount ="0.00"
                                }
                                //多条记录公用一个银行透支
                                $scope.bank = item.bankAmount.groupOverdraft;
                            })
                        }).error(function(data){
                            console.log(data);
                        })
                }
            })
        };


        //下拉刷新列表
        $scope.doRefresh = function() {
            var CCSessionId = localStorage.getItem("CCSessionId");
            //console.log(CCSessionId);
            var url = $rootScope.myglobal.serverurl+'/jsye/jsye.json';
            console.log(url);
            $http({
                method: "POST",
                url: url,
                headers : { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
                data: {
                    CCSessionId:CCSessionId,
                    appMark:1,
                    acctIdsTemp:$scope.acctIdsTemp,
                    groupFlag:"0"
                },
                transformRequest: $rootScope.post
            })
                .success(function(data){
                    console.log(data);
                    angular.forEach(data.maplist,function (ss,index) {
                        //先判断mount.balance是否为null或空，是则取0.00;否则采用千分位计数法显示金额。
                        if(ss.amount==null){
                            //ss.acctIdStr.split(',')[0]
                            angular.forEach($scope.items, function (item,index) {
                                if(item[0]==ss.acctIdStr.split(',')[0]){
                                    item[4]="0.00";
                                }
                            })

                        }else{
                            //金额覆盖更新
                            angular.forEach($scope.items, function (item,index) {
                                if(item[0]==ss.acctIdStr.split(',')[0]){
                                    //console.log(item[4]);
                                  //金额重新赋值
                                    item[4]=ss.amount.balance;
                                    //查询时间重新赋值
                                    $scope.last=ss.amount.queryDateTime.time;
                                    item[5]= $filter("date")($scope.last, "yyyy-MM-dd HH:mm:ss");
                                }
                            })
                        }
                    })

                }).error(function(data){
                    console.log(data);
                })
                .finally(function() {
                    $scope.$broadcast('scroll.refreshComplete');
                });
        };

    })

